package Day14;

import Day12.Test.fileUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GetPanel extends JPanel implements ActionListener {
    private JLabel getLabel = new JLabel("金额");
    private JTextField getField = new JFormattedTextField();
    private JButton sureBtn = new JButton(new ImageIcon("Image/buttonSure.jpg"));
    private JButton cancelBtn = new JButton(new ImageIcon("Image/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,20);
    private ATM05 atm05;
    public GetPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        getLabel.setBounds(250,200,80,50);
        getLabel.setFont(font);
        this.add(getLabel);

        getField.setBounds(380,200,250,50);
        getField.setFont(font);
        this.add(getField);

        sureBtn.setBounds(370,320,140,50);
        sureBtn.addActionListener(this);
        this.add(sureBtn);

        cancelBtn.setBounds(520,320,140,50);
        cancelBtn.addActionListener(this);
        this.add(cancelBtn);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        ArrayList<User> users = atm05.getUsers();
        try {
            double getMoney = Double.parseDouble(getField.getText());
            if(e.getSource() == sureBtn){
                if(getMoney > atm05.getLoginUser().getBlance()){
                    JOptionPane.showMessageDialog(this,"余额不足");
                }else{
                    atm05.getLoginUser().setBlance(atm05.getLoginUser().getBlance() - getMoney);
                    fileUtil.writeObject(users,atm05.getFILE_PATH());
                    JOptionPane.showMessageDialog(this,"取款成功");
                    getField.setText("");
                    atm05.showPanel("menu");
                }
            }
        }catch (NumberFormatException e1){
            JOptionPane.showMessageDialog(this,"只能输入数字");
            getField.setText("");
        }
        if(e.getSource() == cancelBtn){
            getField.setText("");
            atm05.showPanel("menu");
        }
    }
}
